package com.nondo.rdp.finance.endpoint.api.admin;

import com.nondo.rdp.auth.endpoint.model.ConvertCall;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.common.CommUtil;
import com.nondo.rdp.core.common.ValidateUtil;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.Activity;
import com.nondo.rdp.finance.model.activity.ActivityBean;
import com.nondo.rdp.finance.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController("apiAdminActivity")
@RequestMapping("/api/admin/activity")
public class ActivityEndpoint {
    @Autowired
    ActivityService activityService;

    /**
     * 获取活动详情
     *
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping("/info/{id}")
    public View info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {
        View view = new View(20000, "获取数据成功");

        Activity activity = null;
        if (id != null) {
            activity = activityService.findOne(id);
        } else {
            activity = new Activity();
        }

        view.setData(activity);
        return view;
    }

    /**
     * 获取活动列表
     *
     * @param request
     * @param response
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public View<Page<ActivityBean>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Activity> query) {
        View<Page<ActivityBean>> view = new View(20000, "获取列表成功");

        Activity activity = query.getData();

        Page<Activity> page = activityService.findAll(new Page<Activity>(query.getPage(), query.getSize()), activity);

        Page<ActivityBean> resultPage = page.convertPage(new ConvertCall<Activity, Page<ActivityBean>>() {
            @Override
            public Page<ActivityBean> execute(Page<Activity> srcPage) {
                Page<ActivityBean> activityBeanPage = new Page<ActivityBean>(srcPage.getPageNumber(), srcPage.getPageSize(), srcPage.getTotalPages(), srcPage.getTotalElements());

                //处理数据
                List<ActivityBean> list = new ArrayList<ActivityBean>();
                for (Object item : srcPage.getContent()) {
                    ActivityBean activityBean = new ActivityBean();
                    BeanUtils.copy(item, activityBean);

                    activityBean.setMainImg(CommUtil.getFileUrl(activityBean.getMainImg()));

                    list.add(activityBean);
                }
                activityBeanPage.setContent(list);

                return activityBeanPage;
            }
        });

        view.setData(resultPage);

        return view;
    }

    /**
     * 添加活动
     *
     * @param request
     * @param response
     * @param activity
     * @param result
     * @return
     */
    @RequestMapping("/add")
    public View add(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody Activity activity, BindingResult result) {
        View view = new View(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            this.activityService.save(activity);
            view.setData(activity);
        }
        return view;
    }

    /**
     * 更新活动
     *
     * @param request
     * @param response
     * @param activity
     * @param result
     * @return
     */
    @RequestMapping("/update")
    public View update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody Activity activity, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            this.activityService.update(activity);
        }

        return view;
    }

    /**
     * 删除活动
     *
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {
        View view = new View(20000, "删除成功");

        this.activityService.delete(id);

        return view;
    }
}